package com.weai.service;

import com.weai.config.WechatConfig;
import com.weai.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信通知服务类
 * 用于发送微信通知
 */
@Service
@Slf4j
public class WechatNotificationService {

    private final WechatConfig wechatConfig;
    private final HttpClientUtil httpClientUtil;

    @Autowired
    public WechatNotificationService(WechatConfig wechatConfig, HttpClientUtil httpClientUtil) {
        this.wechatConfig = wechatConfig;
        this.httpClientUtil = httpClientUtil;
    }

    /**
     * 发送微信通知
     *
     * @param userName 用户名
     * @param content 通知内容
     * @return 发送结果
     */
    public String sendNotification(String userName, String content) {
        try {
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("keyid", wechatConfig.getKeyid());
            requestBody.put("content", content);
            requestBody.put("type", wechatConfig.getType());
//            requestBody.put("serviceName", wechatConfig.getServiceName());
            requestBody.put("serviceName", "存款AI服务助手");
            requestBody.put("userName", userName);

            log.info("发送微信通知，用户: {}, 内容: {}", userName, content);
            log.info("请求参数: {}", requestBody);
            return httpClientUtil.postForJson(wechatConfig.getUrl(), requestBody);
        } catch (Exception e) {
            log.error("发送微信通知失败，用户: {}, 错误信息: {}", userName, e.getMessage(), e);
            return null;
        }
    }
} 